home *** CD-ROM | disk | FTP | other *** search
/ Chip 2005 August (Alt) / CHIP 2005-08.1.iso / program / guvenlik / syslinux-3.07.exe / mbr.asm < prev    next >
Encoding:
Assembly Source File  |  2004-02-01  |  5.5 KB  |  230 lines

  1. ; -----------------------------------------------------------------------
  2. ;   
  3. ;   Copyright 2003-2004 H. Peter Anvin - All Rights Reserved
  4. ;
  5. ;   Permission is hereby granted, free of charge, to any person
  6. ;   obtaining a copy of this software and associated documentation
  7. ;   files (the "Software"), to deal in the Software without
  8. ;   restriction, including without limitation the rights to use,
  9. ;   copy, modify, merge, publish, distribute, sublicense, and/or
  10. ;   sell copies of the Software, and to permit persons to whom
  11. ;   the Software is furnished to do so, subject to the following
  12. ;   conditions:
  13. ;   
  14. ;   The above copyright notice and this permission notice shall
  15. ;   be included in all copies or substantial portions of the Software.
  16. ;   
  17. ;   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. ;   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  19. ;   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. ;   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  21. ;   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  22. ;   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  23. ;   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  24. ;   OTHER DEALINGS IN THE SOFTWARE.
  25. ;
  26. ; -----------------------------------------------------------------------
  27.  
  28. ;
  29. ; mbr.asm
  30. ;
  31. ; Simple Master Boot Record, including support for EBIOS extensions.
  32. ; The MBR lives in front of the boot sector, and is responsible for
  33. ; loading the boot sector of the active partition.  The EBIOS support
  34. ; is needed if the active partition starts beyond cylinder 1024.
  35. ; This MBR determines all geometry info at runtime.  It uses only the
  36. ; linear block field in the partition table.  It does, however, pass
  37. ; the partition table information unchanged to the target OS.
  38. ;
  39. ; This MBR should be "8086-clean", i.e. not require a 386.
  40. ;
  41.  
  42. %include "bios.inc"
  43.     
  44. ;
  45. ; Note: The MBR is actually loaded at 0:7C00h, but we quickly move it down to
  46. ; 0600h.
  47. ;
  48.         section .text
  49.         cpu 8086
  50.         org 0600h
  51.  
  52. _start:        cli
  53.         xor ax,ax
  54.         mov ds,ax
  55.         mov es,ax
  56.         mov ss,ax
  57.         mov sp,7C00h
  58.         sti
  59.         cld
  60.         mov si,sp        ; Start address
  61.         mov di,0600h        ; Destination address
  62.         mov cx,512/2
  63.         rep movsw
  64.  
  65. ;
  66. ; Now, jump to the copy at 0600h so we can load the boot sector at 7C00h.
  67. ; Since some BIOSes seem to think 0000:7C00h and 07C0:0000h are the same
  68. ; thing, use a far jump to canonicalize the address.  This also makes
  69. ; sure that it is a code speculation barrier.
  70. ;
  71.  
  72.         jmp 0:next        ; Jump to copy at 0600h
  73.                 
  74. next:
  75.         mov [DriveNo], dl        ; Drive number stored in DL
  76. ;
  77. ; Check for CHS parameters.  This doesn't work on floppy disks,
  78. ; but for an MBR we don't care.
  79. ;
  80.         mov ah,08h            ; Get drive parameters
  81.         int 13h
  82.         xor ax,ax
  83.         mov al,dh
  84.         inc ax                ; From 0-based to count
  85.         mov [Heads],ax
  86.         and cl,3Fh            ; Max sector number
  87.         mov [Sectors],cl
  88.         ; Note: we actually don't care about the number of
  89.         ; cylinders, since that's the highest-order division
  90.  
  91. ;
  92. ; Now look for one (and only one) active partition.
  93. ;
  94.         mov si,PartitionTable
  95.         xor ax,ax
  96.         mov cx,4
  97. checkpartloop:
  98.         test byte [si],80h
  99.         jz .notactive
  100.         inc ax
  101.         mov di,si
  102. .notactive:    add si,byte 16
  103.         loop checkpartloop
  104.  
  105.         cmp ax,byte 1            ; Better be only one
  106.         jnz not_one_partition
  107.  
  108. ;
  109. ; Now we have the active partition partition information in DS:DI.
  110. ; Check to see if we support EBIOS.
  111. ;
  112.         mov dl,[DriveNo]
  113.         mov ax,4100h
  114.         mov bx,055AAh
  115.         xor cx,cx
  116.         xor dh,dh
  117.         stc
  118.         int 13h
  119.         jc no_ebios
  120.         cmp bx,0AA55h
  121.         jne no_ebios
  122.         test cl,1            ; LBA device access
  123.         jz no_ebios
  124. ;
  125. ; We have EBIOS.  Load the boot sector using LBA.
  126. ;
  127.         push di
  128.         mov si,dapa
  129.         mov bx,[di+8]            ; Copy the block address
  130.         mov [si+8],bx
  131.         mov bx,[di+10]
  132.         mov [si+10],bx
  133.         mov dl,[DriveNo]
  134.         mov ah,42h            ; Extended Read
  135.         jmp short common_tail
  136. ;
  137. ; No EBIOS.  Load the boot sector using CHS.
  138. ;
  139. no_ebios:
  140.         push di
  141.         mov ax,[di+8]
  142.         mov dx,[di+10]
  143.         div word [Sectors]
  144.         inc dx
  145.         mov cx,dx            ; Sector #
  146.         xor dx,dx
  147.         div word [Heads]
  148.         ; DX = head #, AX = cylinder #
  149.         mov ch,al
  150.         shr ax,1
  151.         shr ax,1
  152.         and al,0C0h
  153.         or cl,al
  154.         mov dh,dl            ; Head #
  155.         mov dl,[DriveNo]
  156.         mov bx,7C00h
  157.         mov ax,0201h            ; Read one sector
  158. common_tail:
  159.         int 13h
  160.         jc disk_error
  161.         pop si                ; DS:SI -> partition table entry
  162. ;
  163. ; Verify that we have a boot sector, jump
  164. ;
  165.         cmp word [7C00h+510],0AA55h
  166.         jne missing_os
  167.         cli
  168.         jmp 0:7C00h            ; Jump to boot sector; far
  169.                         ; jump is speculation barrier
  170.                         ; (Probably not neecessary, but
  171.                         ; there is plenty of space.)
  172.  
  173. not_one_partition:
  174.         ja too_many_os
  175. missing_os:
  176.         mov si,missing_os_msg
  177.         jmp short die
  178. too_many_os:
  179. disk_error:
  180.         mov si,bad_disk_msg
  181. die:
  182. .msgloop:
  183.         lodsb
  184.         and al,al
  185.         jz .now
  186.         mov ah,0Eh            ; TTY output
  187.         mov bh,[BIOS_page]        ; Current page
  188.         mov bl,07h
  189.         int 10h
  190.         jmp short .msgloop
  191. .now:
  192.         jmp short .now
  193.  
  194.         align 4, db 0            ; Begin data area
  195.  
  196. ;
  197. ; EBIOS disk address packet
  198. ;
  199. dapa:
  200.         dw 16                ; Packet size
  201. .count:        dw 1                ; Block count
  202. .off:        dw 7C00h            ; Offset of buffer
  203. .seg:        dw 0                ; Segment of buffer
  204. .lba:        dd 0                ; LBA (LSW)
  205.         dd 0                ; LBA (MSW)
  206.  
  207. ; CHS information
  208. Heads:        dw 0
  209. Sectors:    dw 0
  210.  
  211. ; Error messages
  212. missing_os_msg    db 'Missing operating system', 13, 10, 0
  213. bad_disk_msg    db 'Operating system loading error', 13, 10, 0
  214.  
  215. ;
  216. ; Maximum MBR size: 446 bytes; end-of-boot-sector signature also needed.
  217. ; Note that some operating systems (NT, DR-DOS) put additional stuff at
  218. ; the end of the MBR, so shorter is better.  Location 440 is known to
  219. ; have a 4-byte attempt-at-unique-ID for some OSes.
  220. ;
  221.  
  222. PartitionTable    equ $$+446            ; Start of partition table
  223.  
  224. ;
  225. ; BSS data; put at 800h
  226. ;
  227. DriveNo        equ 0800h
  228.